﻿package mortal.game.view.mining
{
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import fl.data.*;
    import flash.utils.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.view.task.data.*;

    public class MiningCache extends Object
    {
        private var _miningInfos:STaskRoutineTaskTaskInfo;
        private var _miningRefreshInfos:STaskRoutineTaskOtherPlayersInfoList;
        private var _arrBeAskHelp:Array;
        private var _beAskHelpTime:Dictionary;
        private var _beAskHelpList:STaskRoutineTaskOtherPlayersInfoList;
        private var _askedForHelpList:Dictionary;

        public function MiningCache()
        {
            this._arrBeAskHelp = [];
            this._beAskHelpTime = new Dictionary();
            this._askedForHelpList = new Dictionary();
            return;
        }// end function

        public function getTodayStar() : int
        {
            if (this._miningInfos == null)
            {
                return 1;
            }
            return this._miningInfos.star;
        }// end function

        public function clearAskedForHelp() : void
        {
            this._askedForHelpList = new Dictionary();
            return;
        }// end function

        public function setAskedForHel(param1:int) : void
        {
            this._askedForHelpList[param1] = true;
            return;
        }// end function

        public function hasAskForHel(param1:int) : Boolean
        {
            if (this._askedForHelpList.hasOwnProperty(param1))
            {
                return true;
            }
            return false;
        }// end function

        public function get isMaxColor() : Boolean
        {
            return Cache.instance.mining.getTodayColor() == EColor._EColorOrange;
        }// end function

        public function getTodayColor() : int
        {
            if (this._miningInfos == null)
            {
                return 1;
            }
            if (this._miningInfos.color == 0)
            {
                return 0;
            }
            return (this._miningInfos.color - 1);
        }// end function

        public function getTodayDoTaskNum() : int
        {
            if (this._miningInfos == null)
            {
                return 3;
            }
            return this._miningInfos.completeTime;
        }// end function

        public function getTodayHelpOthersNum() : int
        {
            if (this._miningRefreshInfos == null)
            {
                return 0;
            }
            return this._miningRefreshInfos.help;
        }// end function

        public function get hasHelpOthersCount() : Boolean
        {
            return this.getTodayHelpOthersNum() < GameConst.TRoutineTaskRefreshCount;
        }// end function

        public function getMiningTask() : TaskInfo
        {
            var _loc_1:* = Cache.instance.task.getTaskByGroupInTaskDoing(ETaskGroup._ETaskGroupMinning)[0];
            if (_loc_1 == null)
            {
                _loc_1 = Cache.instance.task.getTaskByGroupInTaskCanget(ETaskGroup._ETaskGroupMinning)[0];
            }
            return _loc_1;
        }// end function

        public function getCurTaskRewards() : Array
        {
            var _loc_4:STaskReward = null;
            var _loc_5:SReward = null;
            var _loc_1:Array = [];
            var _loc_2:* = Cache.instance.mining.getMiningTask();
            if (_loc_2 == null)
            {
                return _loc_1;
            }
            var _loc_3:* = _loc_2.stask.rewards;
            if (_loc_3 == null)
            {
                return _loc_1;
            }
            for each (_loc_4 in _loc_3)
            {
                
                if (this._miningInfos != null && !_loc_2.isCanget())
                {
                    for each (_loc_5 in this._miningInfos.rewards)
                    {
                        
                        if (_loc_5.type == _loc_4.reward.type && _loc_5.code == _loc_4.reward.code)
                        {
                            _loc_4.reward.num = _loc_5.num;
                        }
                    }
                }
                _loc_1.push(_loc_4);
            }
            return _loc_1;
        }// end function

        public function getBeRefreshedDataProvider() : DataProvider
        {
            var _loc_1:* = new DataProvider();
            if (this._miningInfos == null)
            {
                return _loc_1;
            }
            var _loc_2:* = this._miningInfos.refreshHistory;
            var _loc_3:* = _loc_2.length - 1;
            while (_loc_3 >= 0)
            {
                
                _loc_1.addItem(_loc_2[_loc_3]);
                _loc_3 = _loc_3 - 1;
            }
            return _loc_1;
        }// end function

        public function getMiningMembersDataProvider() : DataProvider
        {
            var _loc_1:* = new DataProvider();
            if (this._miningRefreshInfos == null)
            {
                return _loc_1;
            }
            var _loc_2:* = this._miningRefreshInfos.otherPlayersInfo;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2.length)
            {
                
                _loc_1.addItem(_loc_2[_loc_3]);
                _loc_3++;
            }
            return _loc_1;
        }// end function

        public function getMiningMembersDataArr() : Array
        {
            var _loc_4:STaskRoutineTaskOtherPlayersInfo = null;
            var _loc_1:Array = [];
            if (this._miningRefreshInfos == null)
            {
                return _loc_1;
            }
            var _loc_2:* = this._miningRefreshInfos.otherPlayersInfo;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2.length)
            {
                
                _loc_4 = _loc_2[_loc_3] as STaskRoutineTaskOtherPlayersInfo;
                _loc_1.push(_loc_4);
                _loc_3++;
            }
            _loc_1.sort(this.sortMiningMember);
            return _loc_1;
        }// end function

        private function sortMiningMember(param1:STaskRoutineTaskOtherPlayersInfo, param2:STaskRoutineTaskOtherPlayersInfo) : int
        {
            if (param1.help >= GameConst.TRoutineTaskRefreshCount)
            {
                return 1;
            }
            if (param2.help >= GameConst.TRoutineTaskRefreshCount)
            {
                return -1;
            }
            if (param1.star == param2.star)
            {
                if (param1.help > param2.help)
                {
                    return 1;
                }
                return -1;
            }
            else if (param1.star < param2.star)
            {
                return 1;
            }
            return -1;
        }// end function

        public function getMiningRefreshStarDataProvider() : DataProvider
        {
            return this.getMiningMembersDataProvider();
        }// end function

        public function getBeAskForHelpTime(param1:int) : int
        {
            return int(this._beAskHelpTime[param1]);
        }// end function

        public function hasBeAskForHelpInfos() : Boolean
        {
            var _loc_3:STaskRoutineTaskOtherPlayersInfo = null;
            var _loc_4:int = 0;
            if (this._arrBeAskHelp == null)
            {
                return false;
            }
            var _loc_1:* = FrameManager.flashFrame.getTime();
            var _loc_2:int = 0;
            while (_loc_2 < this._arrBeAskHelp.length)
            {
                
                _loc_3 = this._arrBeAskHelp[_loc_2];
                if (_loc_3 == null || _loc_3.color == (EColor._EColorOrange + 1))
                {
                }
                else
                {
                    _loc_4 = int(this._beAskHelpTime[_loc_3.player.entityId.id]);
                    if (_loc_1 - _loc_4 >= 60000 && this._beAskHelpTime[_loc_3.player.entityId.id] != null)
                    {
                    }
                    else
                    {
                        return true;
                    }
                }
                _loc_2++;
            }
            return false;
        }// end function

        public function getBeAskHelpDataProvider() : DataProvider
        {
            var _loc_4:STaskRoutineTaskOtherPlayersInfo = null;
            var _loc_5:int = 0;
            var _loc_1:* = new DataProvider();
            if (this._arrBeAskHelp == null)
            {
                return _loc_1;
            }
            var _loc_2:* = FrameManager.flashFrame.getTime();
            var _loc_3:int = 0;
            while (_loc_3 < this._arrBeAskHelp.length)
            {
                
                _loc_4 = this._arrBeAskHelp[_loc_3];
                if (_loc_4 == null || _loc_4.color == (EColor._EColorOrange + 1))
                {
                }
                else
                {
                    _loc_5 = int(this._beAskHelpTime[_loc_4.player.entityId.id]);
                    if (_loc_2 - _loc_5 >= 60000 && this._beAskHelpTime[_loc_4.player.entityId.id] != null)
                    {
                    }
                    else
                    {
                        _loc_1.addItem(_loc_4);
                    }
                }
                _loc_3++;
            }
            return _loc_1;
        }// end function

        public function addAskHelpList(param1:STaskRoutineTaskOtherPlayersInfoList) : void
        {
            var _loc_3:STaskRoutineTaskOtherPlayersInfo = null;
            var _loc_4:Boolean = false;
            var _loc_5:STaskRoutineTaskOtherPlayersInfo = null;
            if (param1 == null)
            {
                return;
            }
            var _loc_2:int = 0;
            while (_loc_2 < param1.otherPlayersInfo.length)
            {
                
                _loc_3 = param1.otherPlayersInfo[_loc_2];
                _loc_4 = false;
                if (_loc_3.color == (EColor._EColorOrange + 1))
                {
                    this._beAskHelpTime[_loc_3.player.entityId.id] = -11111111;
                }
                else
                {
                    this._beAskHelpTime[_loc_3.player.entityId.id] = FrameManager.flashFrame.getTime();
                    for each (_loc_5 in this._arrBeAskHelp)
                    {
                        
                        if (_loc_5.player.entityId.id == _loc_3.player.entityId.id)
                        {
                            _loc_4 = true;
                            break;
                        }
                    }
                    if (!_loc_4)
                    {
                        this._arrBeAskHelp.push(_loc_3);
                    }
                }
                _loc_2++;
            }
            return;
        }// end function

        public function clearBeAskHelp() : void
        {
            this._beAskHelpTime = new Dictionary();
            this._arrBeAskHelp = [];
            return;
        }// end function

        public function delBeAskHelp(param1:STaskRoutineTaskOtherPlayersInfo) : void
        {
            if (this._beAskHelpTime[param1.player.entityId.id] != null)
            {
                delete this._beAskHelpTime[param1.player.entityId.id];
            }
            if (this._arrBeAskHelp == null)
            {
                return;
            }
            var _loc_2:* = this._arrBeAskHelp.indexOf(param1);
            if (_loc_2 < 0)
            {
                return;
            }
            this._arrBeAskHelp.splice(_loc_2, 1);
            return;
        }// end function

        public function get miningRefreshInfos() : STaskRoutineTaskOtherPlayersInfoList
        {
            return this._miningRefreshInfos;
        }// end function

        public function set miningRefreshInfos(param1:STaskRoutineTaskOtherPlayersInfoList) : void
        {
            var _loc_2:STaskRoutineTaskOtherPlayersInfo = null;
            var _loc_3:STaskRoutineTaskOtherPlayersInfo = null;
            this._miningRefreshInfos = param1;
            if (param1.otherPlayersInfo != null && this._arrBeAskHelp != null && this._arrBeAskHelp.length > 0)
            {
                for each (_loc_2 in param1.otherPlayersInfo)
                {
                    
                    for each (_loc_3 in this._arrBeAskHelp)
                    {
                        
                        if (_loc_2.player.entityId.id == _loc_3.player.entityId.id)
                        {
                            _loc_3.beingHelp = _loc_2.beingHelp;
                            _loc_3.color = _loc_2.color;
                            _loc_3.help = _loc_2.help;
                            _loc_3.star = _loc_2.star;
                            _loc_3.taskcode = _loc_2.taskcode;
                        }
                    }
                }
            }
            return;
        }// end function

        public function get miningInfos() : STaskRoutineTaskTaskInfo
        {
            return this._miningInfos;
        }// end function

        public function set miningInfos(param1:STaskRoutineTaskTaskInfo) : void
        {
            this._miningInfos = param1;
            return;
        }// end function

    }
}
